package org.linlinjava.litemall.brand.web;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import org.linlinjava.litemall.brand.annotation.LoginUser;
import org.linlinjava.litemall.brand.service.BrandPermissionService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.ServicePermission;
import org.linlinjava.litemall.db.domain.ServiceRole;
import org.linlinjava.litemall.db.service.ServiceRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 商户承诺
 * @program: mall
 * @author: youvy
 * @create: 2019-12-10 15:25
 **/
@RestController
@RequestMapping("/permission")
public class BrandPermissionController {
	@Autowired
	private BrandPermissionService permissionService;
	@Autowired
	private ServiceRoleService serviceRoleService;

	/**
	 *  商户  ServicePermission 表是 商户角色  后台角色
	 * @param request   MType>0：admin模块，1：brand模块   p_type>  0：接口权限，1页面权限  role_id>客服角色id
	 * @return
	 */
	@PostMapping("/getPage")
	public Object getPagePermission(HttpServletRequest request){
		Integer mType = null;
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		int role = Integer.parseInt(user.getPlugininfo().get("role"));
		String belong = user.getPlugininfo().get("belong");
		if (belong.equals(0+"")){
			mType=0;
		}else {
			mType=1;
		}
		Integer.parseInt(user.getUserid());
		List<ServicePermission> pers = permissionService.findByRoleId(role,mType,1);
		List<String> pList = new ArrayList<>();
		for (ServicePermission per : pers) {
			String permission = per.getPermission();
			pList.add(permission);
		}
		return ResponseUtil.ok(pList);

	}

	/**
	 *   所有角色
	 * @param request  belong是商户的Id
	 * @return
	 */
	@PostMapping("/roleList")
	public Object roleList(HttpServletRequest request){
		Integer mType = null;
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		String belong = user.getPlugininfo().get("belong");
		if (belong.equals(0+"")){
			mType=0;
		}else {
			mType=1;
		}
		List<ServiceRole> serviceRoles = serviceRoleService.listRole(mType);
		return serviceRoles;
	}

/*	@PostMapping("/getApi")
	public Object getApiPermission(HttpServletRequest request){
		Integer mType = null;
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		int role = Integer.parseInt(user.getPlugininfo().get("role"));
		String belong = user.getPlugininfo().get("belong");
		if (belong.equals(0)){
			mType=0;
		}else {
			mType=1;
		}
		Integer.parseInt(user.getUserid());
		List<ServicePermission> pers = permissionService.findByRoleId(role,mType,0);
		List<String> pList = new ArrayList<>();
		for (ServicePermission per : pers) {
			String permission = per.getPermission();
			pList.add(permission);
		}
		return ResponseUtil.ok(pList);
	}*/
}
